package com.isnotlocus.gulimail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.isnotlocus.gulimail.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.isnotlocus.gulimail.product.entity.CategoryEntity;
import com.isnotlocus.gulimail.product.service.CategoryService;
import com.isnotlocus.common.utils.PageUtils;
import com.isnotlocus.common.utils.R;



/**
 * 商品三级分类
 *
 * @author IsnotLocus
 * @email sunlightcs@gmail.com
 * @date 2020-07-16 17:00:57
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;


    //三级查询分类的操作
    @RequestMapping("/list/tree")
    public R TreeCategoryList(@RequestParam Map<String, Object> params){
       List<CategoryEntity> list = categoryService.ListWithTree();

        return R.ok().put("data",list);
    }




    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
   // @RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateById(category);
		if(category.getName()!=null){
            categoryBrandRelationService.updateCategoryDetails(category.getCatId(),category.getName());
        }
        return R.ok();
    }





    /**
     * 批量修改
     */
    @RequestMapping("/update/list")
    // @RequiresPermissions("product:category:update")
    public R updateBatchById(@RequestBody CategoryEntity[] category){
        for(CategoryEntity categoryEntity:category){
            System.out.println(categoryEntity);
        }
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }



    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }


    /**
     * 自定义批量删除
     */


    /**
     * 删除
     *       直接这样删除是物理删除，数据是直接删除的，一般不这么做，
     *       一般要做的就是逻辑删除，就是在数据库给一个字段，然后通过控制那个字段的值
     *        判断删除与否这里就把category的 show_status 作为这个字段
     *       这个时候就在配置
     *       mybatis-plus:
     *          global-config:
     *           db-config:
     *             id-type: auto
     *             logic-delete-value: 1          1代表已经删除的状态
     *             logic-not-delete-value: 0      0代表未删除的状态
     *
     *       然后在categoryentity字段里标注的showStatus 上面注解一个
     *       @TableLogic(value = "1",delval = "0")  第一个指定1显示，后面delval指定不显示的状态
     *       而这里的0 和  1 恰恰就是上面的   logic-delete-value: 1          1代表已经删除的状态
     *      *                               logic-not-delete-value: 0
     *      指定的
     *
     */
    @RequestMapping("/deleteById")
    public R deleteById(@RequestBody Long[] catIds){
        int result= categoryService.deleteByIds(catIds);
        if(result>0){
            System.out.println("删除成功！！！");
        }
        return R.ok();
    }
}
